//
//  ZPAuthEngine.m
//  ZPIOSEducation
//
//  Created by mini1 on 2017/1/9.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "ZPAuthEngine.h"

@implementation ZPAuthEngine

ZPSingletonM(ZPAuthEngine)

#pragma mark - Helper

- (NSInteger)currentUserId{
    if ([[ZPAuthEngine sharedZPAuthEngine] isValid]) {
        ZPAuthStore *store = [ZPAuthStore sharedZPAuthStore];
        return store.memberId;
    }
    return 0;
}


#pragma mark - OAuth

- (BOOL)isValid {
    ZPAuthStore *store = [ZPAuthStore sharedZPAuthStore];
    if ([store.accessToken length] > 10 ) {
        //        BOOL isValid = ![store hasExpired];
        //        return isValid;
        return YES;
    }
    return NO;
}

-(void)registerAndSign{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *homePath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/UserInfo.plist"];
    if (![fileManager fileExistsAtPath:homePath]) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"UserInfo" ofType:@"plist"];
        [fileManager copyItemAtPath:path toPath:homePath error:nil];
    }
    NSMutableDictionary *dataDic = [[NSDictionary dictionaryWithContentsOfFile:homePath] mutableCopy];
    NSString *string = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    if (string && ![string  isEqual:[NSNull  null]]) {
        [dataDic setValue:[NSString  stringWithFormat:@"%@",string] forKey:@"deviceID"];
    }else {
        [dataDic setValue:@"" forKey:@"deviceID"];
    }
    
    
    [dataDic writeToFile:homePath atomically:YES];
    
    [[ZPAuthStore  sharedZPAuthStore] resetUserInfoDictionary];
    [[ZPAuthStore  sharedZPAuthStore] setWebViewCookiesWithType:1];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeNumberOfProductInCart" object:nil];
}

//#pragma mark Public API
//
//- (void)loginWithUsername:(NSString *)userName password:(NSString *)password success:(void (^)(NSMutableDictionary *loginUserDic))success failure:(void (^)(NSString *))failure{
//
//    NSString *isuname = @"1";
//
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    NSDictionary *params = @{@"username": userName,
//                             @"password": password,
//                             @"isuname": isuname,
//                             @"channel":@"3"};//登录逻辑，渠道3
//
//    [manager POST:APIURL@"ucmembers/onLogin.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSMutableDictionary *loginUserDic = [[ NSMutableDictionary alloc] initWithDictionary: (NSDictionary *)responseObject];
//
//        NSTimeZone *zone = [NSTimeZone systemTimeZone];
//        NSDate *date = [NSDate date];
//        NSInteger interval = [zone secondsFromGMTForDate: date];
//        NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
//
//        if ([loginUserDic[@"result"] isEqualToString:@"1"]) {
//            [loginUserDic setValue:password forKey:@"passwd"];
//            [loginUserDic setValue:localeDate forKey:@"expiresIn"];
//            success(loginUserDic);
//        }else{
//            failure(loginUserDic[@"error"]);
//        }
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        failure(@"请求失败请检查网络,稍后重试");
//    }];
//}

@end
